package com.demo.web;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.codehaus.jettison.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.demo.service.HdfsService;

@Controller
public class ExplorerWeb {
	
	@RequestMapping(value="/explorer", method=RequestMethod.GET)
	public String explorer(){
		
		return "explorer";
	}
	
	@Autowired
	private HdfsService hdfsService;
	
	@RequestMapping(value="/explorer/datas", method=RequestMethod.POST)
	@ResponseBody
	public List<Map<String,Object>> getDatas(@RequestParam Map<String, String> params) throws IOException, JSONException{
		String name = params.get("name");
		if("hdfs".equals(name)){
			String dirPath = params.get("dirPath");
			if(StringUtils.isEmpty(dirPath)){
				dirPath = "/";
			}
			List<Map<String, Object>> list = hdfsService.getSubDirFileStatus(dirPath);
			return list;
		}
		List<Map<String,Object>> datas = new ArrayList<Map<String,Object>>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		for(int i=0;i<10;i++){
			Map<String,Object> obj = new HashMap<String,Object>();
			obj.put("id", name+i);
			obj.put("name", name);
			obj.put("ctime", sdf.format(new Date()));
			datas.add(obj);
		}
		return datas;
	}
	
	@RequestMapping(value="/fileStyle", method=RequestMethod.GET)
	public String fileStyle(){
		
		return "fileStyle";
	}

}
